package pyx.crazycards.server.core;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javaactor.library.core.Message;
import javaactor.library.core.Process;
import javaactor.library.core.annotations.Actor;
import javaactor.library.core.exception.JavaActorException;
import pyx.crazycards.server.messages.CrazyCardsMessage;

@Actor(name="ReplyRemote")
public class ReplyRemoteActor extends Process {

	@Override
	public void react(Message message) throws JavaActorException {
		
		try {
			
			send(((CrazyCardsMessage)message).getSender(), 8889, message);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void send(String address, int port, Message msg ) throws UnknownHostException, IOException {
		
			Socket clientSocket = new Socket(address, port);
			ObjectOutputStream stream = new ObjectOutputStream(clientSocket.getOutputStream());			
			stream.writeObject(msg);

	}

}
